using Easislides.Properties;
using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
namespace Easislides
{
	public class FrmImportFolder : Form
	{
		private const int MaxDocExtensions = 3000;
		private static DateTime BuildDocumentsStartTime;
		private static TimeSpan BuildDocumentsLapseTime = new TimeSpan(0L);
		private static bool BuildDocumentsContinue = true;
		private static string[] DocFilesList = new string[32000];
		private static int TotalDocFiles = -1;
		public static string[] DocFileExtension = new string[3000];
		public static int TotalDocFileExt = 0;
		private gf.SongSettings ImportItem = new gf.SongSettings();
		private IContainer components = null;
		private GroupBox groupBox1;
		private ComboBox SongFolder;
		private Label label1;
		private Label label3;
		private Label label4;
		private Label label5;
		private Button BtnCancel;
		private Button BtnOK;
		private ProgressBar ProgressBar1;
		private FolderBrowserDialog folderBrowserDialog1;
		private Panel panel1;
		private ToolStrip toolStrip3;
		private ToolStripButton LocationBtn;
		private TextBox tbLocation;
		public FrmImportFolder()
		{
			this.InitializeComponent();
		}
		private void FrmImportFolder_Load(object sender, EventArgs e)
		{
			this.tbLocation.Text = gf.ImportFolder_StartDir;
			FrmImportFolder.DocFileExtension[0] = ".doc";
			FrmImportFolder.DocFileExtension[1] = ".txt";
			FrmImportFolder.TotalDocFileExt = 2;
			this.BuildFolderList();
		}
		private void LocationBtn_Click(object sender, EventArgs e)
		{
			FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
			folderBrowserDialog.SelectedPath = ((this.tbLocation.Text != "") ? this.tbLocation.Text : "C:\\");
			folderBrowserDialog.Description = "Please select your Source Windows Folder.";
			if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
			{
				this.tbLocation.Text = folderBrowserDialog.SelectedPath;
			}
		}
		private void BtnOK_Click(object sender, EventArgs e)
		{
			if (this.ValidateContents())
			{
				this.StartImport();
			}
		}
		private void StartImport()
		{
			FrmImportFolder.BuildDocumentsContinue = true;
			FrmImportFolder.BuildDocumentsStartTime = DateTime.Now;
			FrmImportFolder.TotalDocFiles = 0;
			FrmImportFolder.BuildDocumentsListArray(this.tbLocation.Text);
			this.ProgressBar1.Value = 0;
			gf.InitialiseIndividualData(ref this.ImportItem);
			int num = 0;
			int num2 = 0;
			int folderNumber = gf.GetFolderNumber(this.SongFolder.Text);
			if (FrmImportFolder.TotalDocFiles > 0 && folderNumber > 0)
			{
				for (int i = 0; i < FrmImportFolder.TotalDocFiles; i++)
				{
					int num3 = i * 100 / FrmImportFolder.TotalDocFiles;
					this.ProgressBar1.Value = ((num3 > 100) ? 100 : num3);
					base.Invalidate();
					this.ProgressBar1.Invalidate();
					string text = Path.GetExtension(FrmImportFolder.DocFilesList[i]).ToLower();
					if (text != null)
					{
						if (!(text == ".doc"))
						{
							if (text == ".txt")
							{
								num2++;
							}
						}
						else
						{
							num++;
						}
					}
					this.ImportItem.FolderNo = folderNumber;
					this.ImportItem.Title = Path.GetFileNameWithoutExtension(FrmImportFolder.DocFilesList[i]);
					this.ImportItem.CompleteLyrics = gf.ExtractDocTextContents(FrmImportFolder.DocFilesList[i]);
					gf.InsertItemIntoDatabase(gf.ConnectStringMainDB, this.ImportItem);
				}
				this.ProgressBar1.Value = 100;
				string text2 = (num > 0) ? (num.ToString() + " Word Document" + ((num > 1) ? "s" : "")) : "";
				string text3 = (num2 > 0) ? (num2.ToString() + " Text File" + ((num2 > 1) ? "s" : "")) : "";
				string text4;
				if (num > 0 && num2 > 0)
				{
					text4 = string.Concat(new string[]
					{
						FrmImportFolder.TotalDocFiles.ToString(),
						" items (",
						text2,
						" and ",
						text3,
						")"
					});
				}
				else
				{
					if (num > 0)
					{
						text4 = text2;
					}
					else
					{
						if (num2 > 0)
						{
							text4 = text3;
						}
						else
						{
							text4 = "";
						}
					}
				}
				MessageBox.Show((text4 != "") ? ("Imported " + text4 + " into " + this.SongFolder.Text) : "No Word/text documents were found for import");
			}
			else
			{
				MessageBox.Show("Nothing Imported.  No Word or text Files were found in the Source Windows Folder");
			}
		}
		public static void BuildDocumentsListArray(string FolderPath)
		{
			if (!(FolderPath == "") && FrmImportFolder.BuildDocumentsContinue && !(!Directory.Exists(FolderPath) | gf.Mid(FolderPath, 1) == ":\\System Volume Information\\"))
			{
				FrmImportFolder.BuildDocumentsLapseTime = DateTime.Now.Subtract(FrmImportFolder.BuildDocumentsStartTime);
				if (FrmImportFolder.BuildDocumentsLapseTime.Seconds > 10)
				{
					FrmImportFolder.BuildDocumentsContinue = false;
				}
				else
				{
					string[] array;
					for (int i = 0; i < FrmImportFolder.TotalDocFileExt; i++)
					{
						try
						{
							string[] files = Directory.GetFiles(FolderPath, "*" + FrmImportFolder.DocFileExtension[i]);
							array = files;
							for (int j = 0; j < array.Length; j++)
							{
								string text = array[j];
								string text2 = text;
								FrmImportFolder.DocFilesList[FrmImportFolder.TotalDocFiles] = text2;
								FrmImportFolder.TotalDocFiles++;
							}
						}
						catch
						{
						}
					}
					string[] directories = Directory.GetDirectories(FolderPath);
					if (directories.Length > 0)
					{
						gf.SingleArraySort(directories, true);
					}
					array = directories;
					for (int j = 0; j < array.Length; j++)
					{
						string str = array[j];
						FrmImportFolder.BuildDocumentsListArray(str + "\\");
					}
				}
			}
		}
		private bool ValidateContents()
		{
			bool result;
			if (!Directory.Exists(this.tbLocation.Text))
			{
				MessageBox.Show("Error - Source Windows Folder does not exist! Please enter a valid Source Windows Folder at A.");
				result = false;
			}
			else
			{
				if (this.SongFolder.Text == "")
				{
					MessageBox.Show("Error - Destination EasiSlides Folder not yet selected! Please select a Destination EasiSlides Folder at B.");
					result = false;
				}
				else
				{
					result = true;
				}
			}
			return result;
		}
		private void BuildFolderList()
		{
			string text = "";
			this.SongFolder.Items.Clear();
			for (int i = 1; i < 41; i++)
			{
				if (gf.FolderUse[i] > 0)
				{
					this.SongFolder.Items.Add(gf.FolderName[i]);
					if (gf.FolderName[i] == gf.ImportFolder_FolderName)
					{
						text = gf.ImportFolder_FolderName;
					}
				}
			}
			this.SongFolder.Text = text;
		}
		private void FrmImportFolder_FormClosing(object sender, FormClosingEventArgs e)
		{
			gf.ImportFolder_FolderName = this.SongFolder.Text;
			gf.ImportFolder_StartDir = this.tbLocation.Text;
		}
		protected override void Dispose(bool disposing)
		{
			if (disposing && this.components != null)
			{
				this.components.Dispose();
			}
			base.Dispose(disposing);
		}
		private void InitializeComponent()
		{
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.panel1 = new System.Windows.Forms.Panel();
            this.toolStrip3 = new System.Windows.Forms.ToolStrip();
            this.LocationBtn = new System.Windows.Forms.ToolStripButton();
            this.tbLocation = new System.Windows.Forms.TextBox();
            this.ProgressBar1 = new System.Windows.Forms.ProgressBar();
            this.label1 = new System.Windows.Forms.Label();
            this.SongFolder = new System.Windows.Forms.ComboBox();
            this.label5 = new System.Windows.Forms.Label();
            this.label4 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.BtnCancel = new System.Windows.Forms.Button();
            this.BtnOK = new System.Windows.Forms.Button();
            this.folderBrowserDialog1 = new System.Windows.Forms.FolderBrowserDialog();
            this.groupBox1.SuspendLayout();
            this.panel1.SuspendLayout();
            this.toolStrip3.SuspendLayout();
            this.SuspendLayout();
            // 
            // groupBox1
            // 
            this.groupBox1.Controls.Add(this.panel1);
            this.groupBox1.Controls.Add(this.tbLocation);
            this.groupBox1.Controls.Add(this.ProgressBar1);
            this.groupBox1.Controls.Add(this.label1);
            this.groupBox1.Controls.Add(this.SongFolder);
            this.groupBox1.Controls.Add(this.label5);
            this.groupBox1.Controls.Add(this.label4);
            this.groupBox1.Controls.Add(this.label3);
            this.groupBox1.Location = new System.Drawing.Point(10, 12);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(520, 182);
            this.groupBox1.TabIndex = 4;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "Import all Text/Word Documents from a particular Windows Folder into a EasiSlides" +
                " Database folder";
            // 
            // panel1
            // 
            this.panel1.Controls.Add(this.toolStrip3);
            this.panel1.Location = new System.Drawing.Point(491, 37);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(23, 23);
            this.panel1.TabIndex = 67;
            // 
            // toolStrip3
            // 
            this.toolStrip3.AutoSize = false;
            this.toolStrip3.CanOverflow = false;
            this.toolStrip3.Dock = System.Windows.Forms.DockStyle.None;
            this.toolStrip3.GripStyle = System.Windows.Forms.ToolStripGripStyle.Hidden;
            this.toolStrip3.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.LocationBtn});
            this.toolStrip3.LayoutStyle = System.Windows.Forms.ToolStripLayoutStyle.Flow;
            this.toolStrip3.Location = new System.Drawing.Point(0, 0);
            this.toolStrip3.Name = "toolStrip3";
            this.toolStrip3.RenderMode = System.Windows.Forms.ToolStripRenderMode.System;
            this.toolStrip3.Size = new System.Drawing.Size(25, 30);
            this.toolStrip3.TabIndex = 0;
            // 
            // LocationBtn
            // 
            this.LocationBtn.AutoSize = false;
            this.LocationBtn.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            this.LocationBtn.Image = global::Properties.Resources.open;
            this.LocationBtn.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.LocationBtn.Name = "LocationBtn";
            this.LocationBtn.Size = new System.Drawing.Size(22, 22);
            this.LocationBtn.Tag = "";
            this.LocationBtn.ToolTipText = "Media File Location";
            this.LocationBtn.Click += new System.EventHandler(this.LocationBtn_Click);
            // 
            // tbLocation
            // 
            this.tbLocation.Location = new System.Drawing.Point(9, 40);
            this.tbLocation.Name = "tbLocation";
            this.tbLocation.Size = new System.Drawing.Size(476, 20);
            this.tbLocation.TabIndex = 66;
            // 
            // ProgressBar1
            // 
            this.ProgressBar1.Location = new System.Drawing.Point(9, 132);
            this.ProgressBar1.Name = "ProgressBar1";
            this.ProgressBar1.Size = new System.Drawing.Size(505, 21);
            this.ProgressBar1.Step = 1;
            this.ProgressBar1.Style = System.Windows.Forms.ProgressBarStyle.Continuous;
            this.ProgressBar1.TabIndex = 58;
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(6, 16);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(0, 13);
            this.label1.TabIndex = 52;
            // 
            // SongFolder
            // 
            this.SongFolder.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.SongFolder.FormattingEnabled = true;
            this.SongFolder.Location = new System.Drawing.Point(9, 85);
            this.SongFolder.MaxDropDownItems = 12;
            this.SongFolder.Name = "SongFolder";
            this.SongFolder.Size = new System.Drawing.Size(199, 21);
            this.SongFolder.TabIndex = 5;
            // 
            // label5
            // 
            this.label5.AutoSize = true;
            this.label5.Location = new System.Drawing.Point(6, 116);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(408, 13);
            this.label5.TabIndex = 56;
            this.label5.Text = "C. Click Import to start the Import. Each title shall be based on the imported Fi" +
                "le name.";
            // 
            // label4
            // 
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(6, 69);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(391, 13);
            this.label4.TabIndex = 55;
            this.label4.Text = "B. Select the destination EasiSlides Folder you wish to store the imported text i" +
                "nto:";
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(6, 24);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(422, 13);
            this.label3.TabIndex = 54;
            this.label3.Text = "A. Select the source Windows Folder which contains the  documents you wish to imp" +
                "ort:";
            // 
            // BtnCancel
            // 
            this.BtnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.BtnCancel.Location = new System.Drawing.Point(450, 200);
            this.BtnCancel.Name = "BtnCancel";
            this.BtnCancel.Size = new System.Drawing.Size(80, 24);
            this.BtnCancel.TabIndex = 9;
            this.BtnCancel.Text = "Close";
            this.BtnCancel.Click += new System.EventHandler(this.BtnCancel_Click);
            // 
            // BtnOK
            // 
            this.BtnOK.Location = new System.Drawing.Point(354, 200);
            this.BtnOK.Name = "BtnOK";
            this.BtnOK.Size = new System.Drawing.Size(80, 24);
            this.BtnOK.TabIndex = 8;
            this.BtnOK.Text = "Import";
            this.BtnOK.Click += new System.EventHandler(this.BtnOK_Click);
            // 
            // FrmImportFolder
            // 
            this.CancelButton = this.BtnCancel;
            this.ClientSize = new System.Drawing.Size(542, 240);
            this.Controls.Add(this.BtnCancel);
            this.Controls.Add(this.BtnOK);
            this.Controls.Add(this.groupBox1);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "FrmImportFolder";
            this.ShowIcon = false;
            this.ShowInTaskbar = false;
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Import Folder";
            this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.FrmImportFolder_FormClosing);
            this.Load += new System.EventHandler(this.FrmImportFolder_Load);
            this.groupBox1.ResumeLayout(false);
            this.groupBox1.PerformLayout();
            this.panel1.ResumeLayout(false);
            this.toolStrip3.ResumeLayout(false);
            this.toolStrip3.PerformLayout();
            this.ResumeLayout(false);

		}

        private void BtnCancel_Click(object sender, EventArgs e)
        {
            base.Close();
        }
	}
}
